#########################################################################################
####################################  FIGURE A6   #######################################
###########     Simulated self-voting rates, by intensity of self-penalty   #############
#########################################################################################

library(pacman)
p_load(tidyverse, msm, sandwich, readxl)
set.seed(12345)
rm(list = ls())

#setwd('C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable')

#########################################################################################


load(file = "simulations/simulations_output_1.Rda")
# open data parameters
load(file = "simulations/simulations_input_params.rda")

getX <- function(y, model) {
  b <- coef(model)[1]
  c <- coef(model)[2]
  d <- coef(model)[3]
  x <- -(sqrt(-4*b*d+ c^2 + 4*d*y) + c)/(2*d)
  return(x)
}


# Set params for graphing
colorFemaleMaj<-"#E69F00"
colorMaleMaj<- "#56B4E9"

loopOut.df.aggregated$prediction <- getX(loopOut.df.aggregated$FSelfvoteRatioBest1F, femaleMajorityTrend)
# graph
ggplot(data = loopOut.df.aggregated) +
  geom_line(aes(x = prediction, y = FSelfvoteRatioBest1F ), color="black", linewidth = 1) +
  # geom_ribbon(aes(x = `Performance penalty for women on themselves (SD)`, ymin=FSelfvoteRatioBest1F_ciLB, ymax=FSelfvoteRatioBest1F_ciUB), linetype=2, alpha=0.1) +
  # geom_line(aes(x = `Performance penalty for women on themselves (SD)`, y = FSelfvoteRatioBest3F, color=colorMaleMaj), linewidth = 1 ) +
  # geom_ribbon(aes(x = `Performance penalty for women on themselves (SD)`, ymin=FSelfvoteRatioBest3F_ciLB, ymax=FSelfvoteRatioBest3F_ciUB), linetype=2, alpha=0.1) +
  geom_point(data = pointsFrame, aes(x = femMajDiscrim, y = femMajValues, color = colorFemaleMaj), size = 3) +
  geom_point(data = pointsFrame, aes(x = maleMajDiscrim, y = maleMajValues, color = colorMaleMaj), size = 3) +
  ylab("self-votes per woman") + xlab("Women's self-penalty (SD)") +
  geom_hline(yintercept = 1, linetype = 'dashed', color="black") +
  geom_segment(aes(x=pointsFrame$femMajDiscrim[1], xend = pointsFrame$femMajDiscrim[1], yend = 0, y = pointsFrame$femMajValues-.01), colour = "#999999", size=.5, linetype="solid", arrow = arrow(length = unit(0.05, "inches"))) +
  geom_segment(aes(x=pointsFrame$maleMajDiscrim[1], xend = pointsFrame$maleMajDiscrim[1], yend = 0, y = pointsFrame$maleMajValues-.01), colour = "#999999", size=.5, linetype="solid", arrow = arrow(length = unit(0.05, "inches"))) +
  scale_color_manual(values=c(colorMaleMaj, colorFemaleMaj ),
                     labels = c("Male-majority voting rate", "Female-majority voting rate")) +
  theme(legend.title=element_blank(), legend.position = c(.9, .85)) +
  theme(text=element_text(size=12),
        plot.title = element_text(hjust=.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour="black"))+
  scale_y_continuous(breaks=seq(0,1.4,.2), expand = c(0,0)) +   scale_x_continuous(breaks=seq(0,max-.15,.15), expand = c(0,0))

ggsave(file = 'figure_a6/figure_a6.pdf', width = 12, height = 7, units = "in")
